package cn.juc;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

/**
 * 线程的分治思想 5+4+3+2+1
 */
public class ForkJoinTest {
    public static void main(String[] args) {
        ForkJoinPool pool=new ForkJoinPool(4);
        Integer rs = pool.invoke(new Fork(5));
        System.out.println(rs);

    }

}
class Fork extends RecursiveTask<Integer>{
   private int n;
    public Fork(int n){
        this.n=n;
    }
    @Override
    protected Integer compute() {
        if (n==1){
            return 1;
        }
         Fork t=new Fork(n-1);
         t.fork();//线程执行
        Integer join = t.join();//计算结果
        return n+join;
    }
}
class AddTask1 extends RecursiveTask<Integer>{

    private int n;
    public AddTask1(int n){
        this.n=n;
    }
    @Override
    protected Integer compute() {
        return n;
    }
}
